package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class SoulboundPane extends AdjustablePanel {
    private JRadioButton rbAll;
    private JRadioButton rbNonbound;
    private JRadioButton rbSoulbound;
    private ButtonGroup group;
    private SelectionCriteria.SoulboundMode defaultMode;

    public SoulboundPane(final int direction) {
        this(direction, SelectionCriteria.SoulboundMode.ALL, true);
    }

    public SoulboundPane(final int direction, final SelectionCriteria.SoulboundMode defaultMode, final boolean enabled) {
        this.defaultMode = defaultMode;
        this.adjustUI();
        this.rbAll.setEnabled(enabled);
        this.rbNonbound.setEnabled(enabled);
        this.rbSoulbound.setEnabled(enabled);
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.rbAll).addComponent(this.rbNonbound).addComponent(this.rbSoulbound));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.rbAll)).addGroup(layout.createParallelGroup().addComponent(this.rbNonbound)).addGroup(layout.createParallelGroup().addComponent(this.rbSoulbound));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.rbAll, this.rbNonbound);
        layout.linkSize(0, this.rbAll, this.rbSoulbound);
        layout.linkSize(1, this.rbAll, this.rbNonbound);
        layout.linkSize(1, this.rbAll, this.rbSoulbound);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntRadio = UIManager.getDefaults().getFont("RadioButton.font");
        if (fntRadio == null) {
            fntRadio = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntRadio = fntRadio.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SOULBOUND_ITEM"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.rbAll == null) {
            this.rbAll = new JRadioButton();
        }
        this.rbAll.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "SOULBOUND_ALL"));
        this.rbAll.setFont(fntRadio);
        if (this.rbNonbound == null) {
            this.rbNonbound = new JRadioButton();
        }
        this.rbNonbound.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "SOULBOUND_NON"));
        this.rbNonbound.setFont(fntRadio);
        if (this.rbSoulbound == null) {
            this.rbSoulbound = new JRadioButton();
        }
        this.rbSoulbound.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "SOULBOUND_ONLY"));
        this.rbSoulbound.setFont(fntRadio);
        if (this.group == null) {
            this.group = new ButtonGroup();
            this.rbAll.setSelected(this.defaultMode.equals(SelectionCriteria.SoulboundMode.ALL));
            this.rbNonbound.setSelected(this.defaultMode.equals(SelectionCriteria.SoulboundMode.NONBOUND));
            this.rbSoulbound.setSelected(this.defaultMode.equals(SelectionCriteria.SoulboundMode.SOULBOUND));
            this.group.add(this.rbAll);
            this.group.add(this.rbNonbound);
            this.group.add(this.rbSoulbound);
        }
    }

    public void clear() {
        this.rbAll.setSelected(true);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        if (this.rbAll.isSelected()) {
            criteria.soulboundMode = SelectionCriteria.SoulboundMode.ALL;
        }
        if (this.rbNonbound.isSelected()) {
            criteria.soulboundMode = SelectionCriteria.SoulboundMode.NONBOUND;
        }
        if (this.rbSoulbound.isSelected()) {
            criteria.soulboundMode = SelectionCriteria.SoulboundMode.SOULBOUND;
        }
    }
}
